package com.edgar.fileupload.library;

import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

/**
 * Created by edgar on 2015/12/29.
 * //文件上传服务,封装线程池
 */
public class FileUploadService extends IntentService{

    private UploadFileManager mUploadFileManager = UploadFileManager.getInstance();

    public FileUploadService(){
        super("FileUploadService");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        readUploadTask();
    }

    /**
     * Queue thread,read upload task for UploadFileManager.
     */
    private void readUploadTask(){
        FileUploadTask fileUploadTask = null;
        while (true){
            fileUploadTask = mUploadFileManager.popUploadTask();
            //If cannot is null,execute task.
            if(fileUploadTask != null){
                fileUploadTask.run();
            } else {
                break;
            }
        }
    }
}